package com.czxy.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czxy.tanhua.dubbo.mappers.*;
import com.czxy.tanhua.entity.*;
import com.czxy.tanhua.vo.BlackListVo;
import com.czxy.tanhua.vo.ResultList;
import com.czxy.tanhua.vo.SettingsVo;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

/**
 * @Author 爱吃豆的土豆、
 * @Date 2023/3/30 14:35
 */
@DubboService
public class UserApiImpl implements UserApi {
    @Resource
    private UserMapper userMapper;

    @Resource
    private SetMapper setMapper;

    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private BlackMapper blackMapper;

    @Resource
    private UserInfoMapper userInfoMapper;
    /**
     * 通过手机号查找用户
     * @param phone
     * @return
     */
    @Override
    public User findByMobile(String phone) {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("mobile",phone);
        User user = this.userMapper.selectOne(userQueryWrapper);
        return user;
    }

    @Override
    public Long save(User user) {
        System.out.println(user);
        this.userMapper.insert(user);
        return user.getId();
    }

    @Override
    public SettingsVo findUsersettings(Long userId) {
        Settings setting = setMapper.findSetting(userId);
        SettingsVo settingsVo = new SettingsVo();
        Long numberLong = new Long(userId);
        Integer id = numberLong.intValue();
        settingsVo.setId(id);
        if (setting.getGonggaoNotification() == 1){
            settingsVo.setGonggaoNotification(true);
        }else {
            settingsVo.setGonggaoNotification(false);
        }
        if (setting.getLikeNotification() == 1){
            settingsVo.setLikeNotification(true);
        }else {
            settingsVo.setLikeNotification(false);
        }
        if (setting.getPinglunNotification() == 1){
            settingsVo.setPinglunNotification(true);
        }else {
            settingsVo.setPinglunNotification(false);
        }

        Question question = questionMapper.findQuestion(userId);
        settingsVo.setStrangerQuestion(question.getTxt());

        return settingsVo;
    }

    @Override
    public void editquestion(String content, Long id) {
        //查询对应的数据
        Question question = questionMapper.findQuestion(id);
        //重新设置
        question.setTxt(content);
        questionMapper.updateById(question);
    }

    @Override
    public void editsetting(SettingsVo settingsVo, Long userId) {
        Settings settings = setMapper.findSetting(userId);
        if (settingsVo.getGonggaoNotification()){
            settings.setGonggaoNotification(1);
        }else {
            settings.setGonggaoNotification(0);
        }

        if (settingsVo.getLikeNotification()){
            settings.setLikeNotification(1);
        }else {
            settings.setLikeNotification(0);
        }

        if (settingsVo.getPinglunNotification()){
            settings.setPinglunNotification(1);
        }else {
            settings.setPinglunNotification(0);
        }
        setMapper.updateById(settings);
    }

    @Override
    public ResultList findblacklist(BlackListVo blackListVo, Long userId) {
        QueryWrapper<BlackList> blackListQueryWrapper = new QueryWrapper<>();
        blackListQueryWrapper.eq("user_id",userId);
        Page<BlackList> blackListPage = new Page<>(blackListVo.getPage(), blackListVo.getPagesize());
        Page<BlackList> blackListPage1 = blackMapper.selectPage(blackListPage, blackListQueryWrapper);

//        //处理起始的索引
//        List<BlackList> allBlack = blackMapper.findAllBlack(blackListVo.getPage() - 1, blackListVo.getPagesize());
        ResultList resultList = new ResultList();
        //设置页大小
        resultList.setPagesize(Math.toIntExact(blackListPage1.getSize()));
        //设置总记录数
        resultList.setCounts(Math.toIntExact(blackListPage1.getTotal()));
        //处理当前页
        resultList.setPage(Math.toIntExact(blackListPage1.getCurrent()));
        //处理总页数
        resultList.setPages(Math.toIntExact(blackListPage1.getPages()));

        for (BlackList record : blackListPage1.getRecords()) {
            UserInfo userInfo = userInfoMapper.selectById(record.getBlackUserId());
            if (userInfo != null){
                record.setAvatar(userInfo.getAvatar());
                record.setNickname(userInfo.getNickname());
                record.setGender(userInfo.getGender());
                record.setAge(Integer.valueOf(userInfo.getAge()));
            }
        }
        resultList.setItems(blackListPage1.getRecords());
        return resultList;
    }

    @Override
    public void deleteblack(Long uid) {
        blackMapper.deleteById(uid);
    }
}
